
package pacman;

import java.io.File;
import java.io.IOException;
import javax.sound.midi.Sequencer;
import javax.sound.sampled.*;

/**
 *
 * @author Khanhoa
 */
public class Sound {
     public static Sequencer player ;
     public static SourceDataLine line;
     public static void PlaySound(String sp){
        SoundPlayer(sp);
    }
    public static void SoundPlayer(String sfn){
    {
        File    soundFile = new File("src\\Data\\" + sfn);
        AudioInputStream audioInputStream = null;
        try
        {
            line.close();
        }
        catch (Exception e) {}
        try
        {
            audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        }
        catch (Exception e) {}
        AudioFormat audioFormat = audioInputStream.getFormat();
        line = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class,audioFormat);
        try
        {
            line = (SourceDataLine) AudioSystem.getLine(info);
            line.open(audioFormat);
        }
        catch (Exception e) {}
        line.start();
        int nBytesRead = 0;
        byte[]  abData = new byte[128000];
        while (nBytesRead != -1)
        {
            try
            {
                nBytesRead = audioInputStream.read(abData, 0, abData.length);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            if (nBytesRead >= 0)
            {
                int nBytesWritten = line.write(abData, 0, nBytesRead);
            }
        }
        }
    }    
}
